from textual.screen import ModalScreen
from textual.widgets import Static, Button
from textual.containers import Vertical, Grid


class FileReadScreen(ModalScreen[bool]):  # 设置界面

    def __init__(self, file_content, file_name) -> None:  # file_name = file_path
        super().__init__() 
        self.file_content = file_content
        self.file_name = file_name
        
    def key_up(self):
        self.query_one("#file_content_container").scroll_up()

    def key_down(self):
        self.query_one("#file_content_container").scroll_down()

    def key_pageup(self):
        self.query_one("#file_content_container").scroll_page_up()

    def key_pagedown(self):
        self.query_one("#file_content_container").scroll_page_down()

    def key_left(self):
        self.query_one("#file_content_container").scroll_left()
        
    def key_right(self):
        self.query_one("#file_content_container").scroll_right()

    def compose(self):
        yield Grid(
                Static(self.file_name, id="file_title"),
                Vertical(Static("", id="file_content"),id="file_content_container"),
                Button("编辑", variant="success", id="file_edit"),
                Button("退出", variant="error", id="file_quit"),
                id="file_window",
            )
        
    def on_mount(self):
        data = self.query_one("#file_content", Static)
        data.update(self.file_content)
        self.query_one("#file_content").scroll_home(animate=False)

    def on_button_pressed(self, event: Button.Pressed) -> None:
        if event.button.id == "file_quit":
            self.dismiss(False)
        else:  # 编辑
            self.dismiss(True)
